#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;

int GetMonthDay(int year, int month)
{
	int MonthDay[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0))
	{
		return MonthDay[month] + 1;
	}
	else
		return MonthDay[month];
}

int main()
{
	int m;
	int _year, _month, _day, day;
	cin >> m;
	for (int i = 0; i<m; i++)
	{
		cin >> _year >> _month >> _day >> day;
		_day += day;
		while (_day>GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month == 13)
			{
				_year++;
				_month = 1;
			}
		}
		printf("%d-%02d-%02d\n", _year, _month, _day);
	}
	return 0;
}